package com.hs.srm.admin.modules.inforecord.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.common.util.ExcelHelpUtils;
import com.hs.srm.admin.modules.inforecord.VO.SupplyGoodVO;
import com.hs.srm.admin.modules.inforecord.dao.SupplyGoodDao;
import com.hs.srm.admin.modules.inforecord.entity.SupplyGoodEntity;
import com.hs.srm.admin.modules.inforecord.service.SupplyGoodService;
import com.hs.srm.admin.modules.material.dao.MaterialFactoryDao;
import com.hs.srm.admin.modules.vendor.dao.VendorDao;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sunnykid.text.SunnykidText;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author socik
 * @date 2019/6/13 13:22
 * @email socik@qq.com
 */
@Service
public class SupplyGoodServiceImpl extends ServiceImpl<SupplyGoodDao, SupplyGoodEntity> implements SupplyGoodService {
    @Autowired
    SupplyGoodDao supplyGoodDao;
    @Autowired
    VendorDao vendorDao;
    @Autowired
    MaterialFactoryDao materialDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SupplyGoodEntity> page = this.selectsgDetailPage(new QueryParam<SupplyGoodEntity>(params).getPage(),
                new EntityWrapper<SupplyGoodEntity>(), params);
        return new PageUtils(page);
    }

    private Page<SupplyGoodEntity> selectsgDetailPage(Page<SupplyGoodEntity> page, Wrapper<SupplyGoodEntity> wrapper,
                                                      Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectSgPage(page, wrapper, params));
        return page;
    }


    @Override
    public SupplyGoodEntity selectByCode(String materilCode, String srmCode, String werks) {
        EntityWrapper<SupplyGoodEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", materilCode).eq("srm_code", srmCode).eq("werks", werks).and("toSendSap!=-1");
        SupplyGoodEntity supplyGoodEntity = this.selectOne(queryWrapper);
        return supplyGoodEntity;
    }

    @Override
    public PageUtils vendorFromSupplyGoods(Map<String, Object> params) {
        Page<SupplyGoodVO> page = this.selectSGDetailPage(new QueryParam<SupplyGoodVO>(params).getPage(),
                new EntityWrapper<SupplyGoodVO>(), params);
        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> importData(File savedFile, String username) {

        Map<String, Object> m = new HashMap<String, Object>();
        List<SupplyGoodEntity> list = ExcelHelpUtils.readExcel(savedFile.getAbsolutePath(), SupplyGoodEntity.class);
        for (SupplyGoodEntity v : list) {
            v.setPkId(SunnykidText.getUUID());
            String vendorCode = v.getSrmCode();
            if (this.vendorDao.findBySrmCode(vendorCode) != null) {
                String vendorName = this.vendorDao.findBySrmCode(vendorCode).getVendorName();
                v.setVendorName(vendorName);
            } else {
                v.setVendorName(null);
            }
            String materialCode = v.getMaterialCode();
            if (this.materialDao.findByMaterialCode(materialCode) != null) {
                String materialDesc = this.materialDao.findByMaterialCode(materialCode).getMaterialDesc();
                v.setMaterialDesc(materialDesc);
            } else {
                v.setMaterialDesc(null);
            }
            this.updateById(v);
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("impSupplyGoodList", list);
        return m;
    }

    @Override
    public int saveImp() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        List<SupplyGoodEntity> dataList = (List<SupplyGoodEntity>) session.getAttribute("impSupplyGoodList");
        if (dataList == null) {
            throw new RuntimeException("数据已经过期，请重新上传导入！");
        }
        int x = 0;
        for (SupplyGoodEntity ven : dataList) {
            x += this.baseMapper.insert(ven).intValue();
        }
        session.removeAttribute("impInforecordList");// 导入完成后，移除对应的session
        return x;
    }

    private Page<SupplyGoodVO> selectSGDetailPage(Page<SupplyGoodVO> page, Wrapper<SupplyGoodVO> wrapper,
                                                  Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectSGDetailPage(page, wrapper, params));
        return page;
    }

}
